package com.egreen.pointsmall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.egreen.common.pojo.VO.pointsmall.ProductCategoryVO;
import com.egreen.common.pojo.entity.pointsmall.Product;
import com.egreen.common.pojo.entity.pointsmall.ProductCategory;
import com.egreen.pointsmall.service.IProductCategoryService;
import com.egreen.pointsmall.dao.ProductDao;

import com.egreen.pointsmall.dao.ProductCategoryDao;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author yyf
 * @since 2023-04-05
 */
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryDao, ProductCategory> implements IProductCategoryService {
     @Autowired
     private ProductCategoryDao productCategoryDao;

     @Autowired
     private ProductDao productDao;
    /**
     * 构建商品分类菜单
     * @return
     */
    @Override
    public List<ProductCategoryVO> buildProductCategoryMenu() {
        //查询所有商品分类数据
        List<ProductCategory> productCategoryList = productCategoryDao.selectList(null);
        //使用stream流将ProductCategory数据类型转换成ProductCategoryVO
        List<ProductCategoryVO> productCategoryVOList = productCategoryList.stream().map(ProductCategoryVO::new).collect(Collectors.toList());
        //进行父子集菜单的封装
        List<ProductCategoryVO> setchildrens = setchildrens(productCategoryVOList);
        //查找一级分类对应的商品信息
        /*getLevelOneProduct(setchildrens);*/
        return setchildrens;
    }

    @Override
    public List<ProductCategoryVO> findAllProductByCategoryLevelOne() {
        QueryWrapper<ProductCategory>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("type",1);
        List<ProductCategory> productCategoryList = productCategoryDao.selectList(queryWrapper);
        //使用stream流将ProductCategory数据类型转换成ProductCategoryVO
        List<ProductCategoryVO> productCategoryVOList = productCategoryList.stream().map(ProductCategoryVO::new).collect(Collectors.toList());
        getLevelOneProduct(productCategoryVOList);
        return productCategoryVOList;
    }

    /**
     * 查询一级商品分类所对应的商品信息
     */
    public void getLevelOneProduct(List<ProductCategoryVO>list){
        for (ProductCategoryVO productCategoryVO : list) {
            QueryWrapper<Product>queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("categorylevelone_id",productCategoryVO.getId());
            List<Product> products = productDao.selectList(queryWrapper);
            productCategoryVO.setProductList(products);
        }
    }

    /**
     * 构建子集菜单
     */
    public List<ProductCategoryVO> setchildrens(List<ProductCategoryVO> list){
        //找到一级菜单,采用stream流进行查询得到parent_id=0的菜单
        List<ProductCategoryVO> categorylevelOneList =
               list.stream().filter(c -> c.getParentId() == 0).collect(Collectors.toList());
        for (ProductCategoryVO productCategoryVO : categorylevelOneList) {
            recursion(list,productCategoryVO);
        }
        return categorylevelOneList;
    }

    /**
     * 进行递归分类
     * @param list
     * @param vo
     */
    public void recursion(List<ProductCategoryVO>list,ProductCategoryVO vo){
        List<ProductCategoryVO> children= getChildrens(list, vo);
        vo.setChildren(children);
        if (children.size()>0){
            //表示当前菜单存在二级菜单
            for (ProductCategoryVO child : children) {
                recursion(list,child);
            }
        }
    }

    /**
     * 获取子菜单
     * @param list
     * @param vo
     * @return
     */
    public List<ProductCategoryVO> getChildrens(List<ProductCategoryVO>list,ProductCategoryVO vo){
        List<ProductCategoryVO>children=new ArrayList<>();
        //使用迭代器进行遍历
        Iterator<ProductCategoryVO> iterator = list.iterator();
        while(iterator.hasNext()){
            ProductCategoryVO next = iterator.next();
            //获取当前对象的子菜单
            if (next.getParentId().equals(vo.getId())){
              children.add(next);
            }
        }
        return children;
    }

}
